package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.enums.OperaPswEnum;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.object.pojo.UpKeepConfig;
import com.tbit.uqbike.object.pojo.vo.UpKeepConfigReqVO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.OperaPswService;
import com.tbit.uqbike.service.business.UpKeepConfigService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.BeanUtils;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 保养配置
 * @author yao
 */
@RestController
@RequestMapping("/upKeepConfig")
public class UpKeepConfigController {

    @Resource
    private UpKeepConfigService upKeepConfigService;
    @Autowired
    private OperaPswService operaPswService;

    // 保养配置
    @RequestMapping("/edit")
    @OperateLog(module = ModuleEnum.SYSTEM_CONFIGS, methods = MethodEnum.UN_KEEP_CONFIG_EDIT)
    public JsonResult edit(@RequestBody @Valid UpKeepConfigReqVO reqVO) {
        LoginInfo loginInfo = BaseController.getLogin(ServletUtils.getRequest());
        if (loginInfo.type.equals(UserConstant.USER_AGENT)) {
            reqVO.setAccountId(loginInfo.id);
        } else {
            Assert.notNull(reqVO.getAccountId(), "ver.accountId.notNull");
        }
        /**操作密码校验*/
        operaPswService.checkOperaPsw(loginInfo, OperaPswEnum.OPERA_PSW_SYSTEM_CONFIG.getModule(), reqVO.getOperaPsw());

        UpKeepConfig upKeepConfig = BeanUtils.copyProperties(reqVO, UpKeepConfig.class);

        Date now = new Date();
        upKeepConfig.setCreator(loginInfo.getAccountUserId().toString());
        upKeepConfig.setCreateTime(now);
        upKeepConfig.setUpdater(loginInfo.getAccountUserId().toString());
        upKeepConfig.setUpdateTime(now);

        upKeepConfigService.createOrUpdate(upKeepConfig);

        return JsonResult.ok();
    }


    /**
     * 获取
     */
    @RequestMapping("/getByAccount")
    public JsonResult getByAccount(@NotNull(message = "ver.accountId.notNull") Integer accountId) {
        LoginInfo loginInfo = BaseController.getLogin(ServletUtils.getRequest());

        if (loginInfo.type.equals(UserConstant.USER_AGENT)) {
            accountId = loginInfo.id;
        } else {
            Assert.notNull(accountId, "ver.accountId.notNull");
        }

        UpKeepConfig upKeepConfig = upKeepConfigService.getByAccountId(accountId);

        return JsonResult.ok(upKeepConfig);
    }
}
